package com.government.sequence.key;

import cn.hutool.core.util.StrUtil;

import java.util.concurrent.TimeUnit;

public abstract class BaseKeyGenerator implements KeyGenerator {

    private TimeUnit unit = TimeUnit.SECONDS;

    private long expireSeconds;

    private String prefix;

    private String separator;


    public BaseKeyGenerator(String prefix) {
        this(0, prefix, StrUtil.COLON);
    }

    public BaseKeyGenerator(long expireSeconds, String prefix) {
        this(expireSeconds, prefix, StrUtil.COLON);
    }

    private BaseKeyGenerator(long expireSeconds, String prefix, String separator) {
        this.expireSeconds = expireSeconds;
        this.prefix = prefix;
        this.separator = separator;
    }


    protected void setExpireSeconds(long expireSeconds) {
        this.expireSeconds = expireSeconds;
    }

    @Override
    public String getKey() {
        String simpleName = getClass().getSimpleName();
        return simpleName + separator + prefix;
    }

    /**
     * 0代表永不过期
     *
     * @return
     */
    @Override
    public long expireSeconds() {
        return expireSeconds;
    }

    public TimeUnit getUnit() {
        return unit;
    }
}